<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/taglibs.jsp" %>
<table id="mp_treegrid" class="easyui-treegrid"  title="列表"
       data-options="
            	border:false,
            	fit:true,
                iconCls: 'icon-ok',
                collapsible: true,
                fitColumns: true,
                url: '${ctx}/Wechat/treegrid.do',
                method: 'post',
                idField: 'id',
                parentField : 'pid',
                treeField: 'text',
                onLoadSuccess:function(){
                    $.messager.progress('close');
                },
                toolbar : [ {
					text : '添加',
					iconCls : 'icon-add',
					handler : function() {
						mp_add_Fun();
					}
					}, '-', {
						text : '编辑',
						iconCls : 'icon-edit',
						handler : function() {
							mp_edit_fun();
						}
					}, '-', {
						text : '删除',
						iconCls : 'icon-remove',
						handler : function() {
							mp_removeDd();
						}
					} , '-', {
						text : '折叠',
						iconCls : 'resultset_previous',
						handler : function() {
							collapseDdAll();
						}
					} , '-', {
						text : '展开',
						iconCls : 'resultset_next',
						handler : function() {
							expandDdAll();
						}
					} , '-', {
						text : '生成自定义菜单',
						iconCls : 'icon-add',
						handler : function() {
							createselfmenu();
						}
					}]
            ">
    <thead>
    <tr>
        <th data-options="field:'text',width:60">菜单名称</th>
        <th data-options="field:'value',width:60">菜单名称值</th>
        <th data-options="field:'pname',width:60">父级菜单名称</th>
        <th data-options="field:'menums',width:60">菜单描述</th>
        <th data-options="field:'legnumber',width:60">菜单层级</th>
    </tr>
    </thead>
</table>
<script>
    var wins4ddTreeGrid = $.messager.progress({
        title:'提示',
        msg:'正在加载案件,请等待'
    });
		var treegrid = $("#mp_treegrid");

        function collapseDdAll(){
            $('#mp_treegrid').treegrid('collapseAll');
        }
        function expandDdAll(){
            $('#mp_treegrid').treegrid('expandAll');
        }

    function mp_add_Fun(){
        var mp_add_dialog  = $("<div/>").dialog({
            width: 700,
            height: 340,
            title: '添加菜单',
            closed: false,
            cache: false,
            resizable : true,
            href: '${ctx}/Wechat/addTo.do',
            modal: true,
            buttons:[{
                text : '保存',
                iconCls: 'icon-add',
                handler : function() {
                    $.ajax({
                        type : "post",
                        url : '${ctx}/Wechat/save.do',
                        data : $.serializeObject($('#mp_add_Form')),
                        dataType : "json",
                        beforeSend: function(){
                            //alert($("#pid").combobox('getValue'));
                            return $('#mp_add_Form').form('validate');
                        },
                        success : function(data) {
                            if(data.success){
                                $('#mp_treegrid').treegrid('load');//此方法还得发送一次查询的请求效率较低，所以要用下面的方法替代
                                $('.easyui-combotree').combotree("clear");
                                mp_add_dialog.dialog('close');
                            }
                            $.messager.show({
                                title:'提示',
                                msg:data.msg,
                                showType:'slide',
                                style:{
                                    right:'',
                                    top:document.body.scrollTop+document.documentElement.scrollTop,
                                    bottom:''
                                }
                            });
                        }
                    });
                }
            } ],
            onClose:function(){
                $(this).dialog('destroy');
            },
            onLoad:function(){
                $("#mp_add_Form").form('load',node);
                $( '#mp_add_Form').treegrid( 'reload');
            }
    });
    }

    window.menu_pid = '';//设置全局变量，注意menu_pid必须是唯一的,再看下面181行window.menu_pid = rows.pid; ，后端看TreeController147行
    function mp_edit_fun(){
        var treegrid = $("#mp_treegrid");
        var rows = treegrid.treegrid('getSelected');//注意这里是treegrid
        console.log(rows);
        if(rows == undefined){
            $.messager.show({
                title:'提示',
                msg:'请您选择一条数据来做编辑操作！'
            });
        }
        if(rows){
            var menu_edit_dialog  = $("<div/>").dialog({
                title: '编辑',
                width: 700,
                height: 340,
                closed: false,
                cache: false,
                resizable : true,
                href: '${ctx}/Wechat/editTo.do?id='+rows.id,
                modal: true,
                buttons:[{
                    text : '保存',
                    iconCls: 'icon-add',
                    handler : function() {
                        $.ajax({
                            type : "post",
                            url : '${ctx}/Wechat/edit.do',
                            data : $.serializeObject($('#menu_edit_Form')),
                            dataType : "json",
                            beforeSend: function(){
                                return $('#menu_edit_Form').form('validate');
                            },
                            success : function(data) {
                                if(data.success){
                                    $('#mp_treegrid').treegrid('load');//此方法还得发送一次查询的请求效率较低，所以要用下面的方法替代
                                    menu_edit_dialog.dialog('close');
                                }
                                $.messager.show({
                                    title:'提示',
                                    msg:data.msg,
                                    showType:'slide',
                                    style:{
                                        right:'',
                                        top:document.body.scrollTop+document.documentElement.scrollTop,
                                        bottom:''
                                    }
                                });
                            }
                        });
                    }
                } ],
                onClose:function(){
                    $(this).dialog('destroy');
                },
                onLoad:function(){
                    console.log(rows.type);
                    $("#menu_edit_Form").form('load',rows);
                    console.log(rows)
                    if(rows.pid){
                        window.menu_pid = rows.pid;
                    }
                    if(rows.type=='view'){
                        $("#urltr").show();
                        $("#isoutlinktr").show();
                    }else{
                        $("#urltr").hide();
                        $("#isoutlinktr").hide();
                    }
                }
            });
        }
    }

    	function createselfmenu() {
            $.ajax({
                url:'${ctx}/Wechat/wechatmenu',
                type : "get",
                dataType:'json',
                success:function(data){
                    $.messager.show({
                        title:'提示',
                        msg:data.msg
                    });
                }
            });
    	}
    function mp_removeDd() {
        var treegrid = $("#mp_treegrid");
        var node = treegrid.treegrid('getSelected');
        if(node!=null){
            $.messager.confirm('确认','确认要要删除吗？',function(r){
                if (r){
                    $.ajax({
                        url:'${ctx}/Wechat/del',
                        data:{
                            ids:node.id
                        },
                        dataType:'json',
                        success:function(data){
                            treegrid.treegrid('reload');
                            treegrid.treegrid('unselectAll');//使全部的行形成未選中狀態
                            $.messager.show({
                                title:'提示',
                                msg:data.msg
                            });
                        }
                    });
                }
            });
        }else{
            $.messager.alert('提示','请选择一行来做删除操作！','info');
        }
    }
    </script>


